package io.renren.modules.yst.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.sys.controller.AbstractController;
import io.renren.modules.yst.form.WorkerCardCreateForm;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.renren.modules.yst.entity.WorkerCardEntity;
import io.renren.modules.yst.service.WorkerCardService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 司机工作卡
 *
 * @author lazyrm
 * @email lazyrm0720@gmail.com
 * @date 2022-01-07 21:01:23
 */
@RestController
@RequestMapping("yst/workercard")
public class WorkerCardController extends AbstractController {
    @Autowired
    private WorkerCardService workerCardService;



    @PostMapping("/create")
    @ApiOperation("司机登记")
    public R createWorderCard(@RequestBody WorkerCardCreateForm form){
        //表单校验
        ValidatorUtils.validateEntity(form);
        WorkerCardEntity workerCardEntity=new WorkerCardEntity();
        workerCardEntity.setWorkerId(getWorkerId());
        workerCardEntity.setCarNumber(form.getCarNumber());
        workerCardEntity.setCarNumberPic(form.getCarNumberPic());
        workerCardEntity.setCreateTime(new Date());
        workerCardService.saveOrUpdate(workerCardEntity);
        return R.ok();
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("yst:workercard:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = workerCardService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{cardId}")
    @RequiresPermissions("yst:workercard:info")
    public R info(@PathVariable("cardId") Long cardId){
		WorkerCardEntity workerCard = workerCardService.getById(cardId);

        return R.ok().put("workerCard", workerCard);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("yst:workercard:save")
    public R save(@RequestBody WorkerCardEntity workerCard){
		workerCardService.save(workerCard);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("yst:workercard:update")
    public R update(@RequestBody WorkerCardEntity workerCard){
		workerCardService.updateById(workerCard);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("yst:workercard:delete")
    public R delete(@RequestBody Long[] cardIds){
		workerCardService.removeByIds(Arrays.asList(cardIds));

        return R.ok();
    }

}
